Option Explicit

REM This script copies the binaries from their output directories,
REM then creates a zip file.


Sub MakeRelease(strSuffix)
   Dim objArgs, objArgsUnnamed, objArgsNamed
   Dim objShell, objExec, objStdOut, strCmd, strLine
   Dim strArg, reArg
   Dim strPath
   Dim objProjects
   Dim objDVDUtil
   Dim objReleases
   Dim objTarget
   'Dim objFile
   'Dim objBin
   'Dim objDLL
   Dim i, m, mm, s, ss
   Dim strWinZip, strCWD

   Set objArgs = Wscript.Arguments

   If objArgs.Count <= 0 Then
      Wscript.Echo "no arguments specified"
      Wscript.Quit
   End If

   Set objArgsNamed = objArgs.Named

   If objArgsNamed.Count Then
      Wscript.Echo "no named arguments are supported"
      Wscript.Quit
   End If

   Set objArgsUnnamed = objArgs.Unnamed

   If objArgsUnnamed.Count <> 1 Then
      Wscript.Echo "wrong number of (unnamed) arguments"
      Wscript.Quit
   End If

   strArg = objArgsUnnamed(0)

   Set reArg = New RegExp
   reArg.Pattern = "^(\d+)\.(\d+)\.(\d+)$"

   Set mm = reArg.Execute(strArg)

   If mm.Count <> 1 Then
      Wscript.Echo "version specifier has bad syntax:", strArg
      Wscript.Quit
   End If

   Set m = mm(0)  'the match
   Set ss = m.Submatches

   If ss.Count <> 3 Then
      Wscript.Echo "internal error #1"
      Wscript.Quit
   End If

   For i = 0 To 2
      If len(ss(i)) <= 0 Then
	 Wscript.Echo "version specifier has bad syntax:", strArg
	 Wscript.Quit
      End If

      If Not IsNumeric(ss(i)) Then
	 Wscript.Echo "internal error #2"
	 Wscript.Quit
      End If

      If CInt(ss(i)) < 0 Then
	 Wscript.Echo "version specifier has bad syntax:", strArg
	 Wscript.Quit
      End If
   Next   

   If CInt(ss(0)) <= 0 Then
      Wscript.Echo "major release number must be greater than 0"
      Wscript.Quit
   End If   

   strPath = objFSO.GetParentFolderName(Wscript.ScriptFullName)
   'Wscript.Echo "Script.Folder:", strPath

   Set objDVDUtil = objFSO.GetFolder(strPath)

   If StrComp(objDVDUtil.Name, "dvdutil", 1) <> 0 Then
      Wscript.Echo "internal error #3"
      Wscript.Quit
   End If

   Set objProjects = objDVDUtil.ParentFolder

   If StrComp(objProjects.Name, "Projects", 1) <> 0 Then
      Wscript.Echo "internal error #4"
      Wscript.Quit
   End If   

   strPath = objFSO.Buildpath(objProjects, "releases")

   If objFSO.FolderExists(strPath) Then
      Set objReleases = objFSO.GetFolder(strPath)
   Else   
      Wscript.Echo "Creating target releases folder"
      Set objReleases = objFSO.CreateFolder(strPath)
   End If   

   strPath = objFSO.BuildPath(objReleases.Path, "dvdutil")

   If objFSO.FolderExists(strPath) Then
      Set objReleases = objFSO.GetFolder(strPath)
   Else
      Wscript.Echo "Creating releases/dvduil folder"
      Set objReleases = objFSO.CreateFolder(strPath)
   End If   

   strPath = objFSO.BuildPath(objReleases.Path, "dvdutil-" & strArg)

   If objFSO.FolderExists(strPath) Then
      Wscript.Echo "Target release folder """ & objFSO.GetFileName(strPath) & """ already exists"
      Set objTarget = objFSO.GetFolder(strPath)
   Else
      Wscript.Echo "Creating target release folder """ & objFSO.GetFileName(strPath) & """"
      Set objTarget = objFSO.CreateFolder(strPath)
   End If   

   strPath = objFSO.BuildPath(objDVDUtil.Path, "mkvmux")

   If Not objFSO.FolderExists(strPath) Then
      Wscript.Echo "internal error #5"
      Wscript.Quit
   End If

   strPath = objFSO.BuildPath(strPath, "README.TXT")

   If not objFSO.FileExists(strPath) Then
      Wscript.Echo "README.TXT not found"
      Wscript.Quit
   End If

   Call objFSO.CopyFile(strPath, objFSO.BuildPath(objTarget.Path, "README.TXT"))
   Call CopyBinary(objProjects, "mkvmux.dll", strSuffix, objTarget)
   Call CopyBinary(objProjects, "vtsread.dll", strSuffix, objTarget)
   Call CopyBinary(objProjects, "vob2mkv.exe", strSuffix, objTarget)

   'For Each objFile In objTarget.Files
   '   Wscript.Echo "target file:", objFile.Name
   'Next 

   REM Is there a way to inspect the version resource from within a script?

   REM The command we want is:
   REM wzzip -P -r -ex -m vob2mkv-1.0.0.zip vob2mkv-1.0.0
   REM We might not need the -r switch, since the directory is flat.
   REM The switch -ex says use best compression, but maximize portability.
   REM (It does not require .zipx file extension.)
   REM The -m switch moves the files, which means delete them.  The containing
   REM directory is also deleted, if it becomes empty (true in this case).

   Set objShell = CreateObject("Wscript.Shell")

   strWinzip = objShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%")
   'Wscript.Echo "program files path:", strWinZip

   If Not objFSO.FolderExists(strWinZip) Then
      Wscript.Echo "Program Files folder not found"
      Wscript.Quit
   End If

   strWinZip = objFSO.BuildPath(strWinZip, "WinZip")
   'Wscript.Echo "WinZip folder path:", strWinZip

   If Not objFSO.FolderExists(strWinZip) Then
      Wscript.Echo "WinZip folder not found"
      Wscript.Quit
   End If
   
   strWinZip = objFSO.BuildPath(strWinZip, "wzzip.exe")
   Wscript.Echo "WinZip command-line app path:", strWinZip

   If Not objFSO.FileExists(strWinZip) Then
      Wscript.Echo "WinZip command-line app not found"
      Wscript.Quit
   End If

   Wscript.Echo "objTarget.Path:", objTarget.Path
   Wscript.Echo "objTarget.Name:", objTarget.Name

   strCWD = objTarget.ParentFolder.Path
   Wscript.Echo "objTarget.Parent.Path:", strCWD
   Wscript.Echo

   strCmd = """" & strWinZip & """ -P -ex -m """

   strCmd = strCmd & _
	     objFSO.BuildPath(objTarget.ParentFolder.Path, _
			      "vob2mkv-" & strArg & ".zip")

   strCmd = strCmd & """ " & objTarget.Name 

   'Wscript.Echo strCmd

   objShell.CurrentDirectory = strCWD

   Set objExec = objShell.Exec(strCmd)
   Set objStdOut = objExec.StdOut

   Do Until objStdOut.AtEndOfStream
      strLine = objStdOut.ReadLine
      Wscript.Echo strLine

      If objExec.Status Then
	 Exit Do
      End If      
   Loop
End Sub


Sub CopyBinary(objProjects, strFile, strSuffix, objTarget)
   Dim strExt, strTargetFilePath
   Dim strSolution, strProject
   Dim objFile

   strExt = objFSO.GetExtensionName(strFile)

   If StrComp(strExt, "exe", 1) = 0 Then     
      strPath = objFSO.BuildPath(objProjects, "bin")  'TODO: change name?

   ElseIf StrComp(strExt, "dll", 1) = 0 Then     
      strPath = objFSO.BuildPath(objProjects, "dll")  'TODO: create these under bin?

   Else
      Wscript.Echo "source file has bad extension:", strFile
      Wscript.Quit      
   End If

   If Not objFSO.FolderExists(strPath) Then
      Wscript.Echo "Source folder """ & strPath & """ not found"
      Wscript.Quit
   End If

   strSolution = "dvdutil"

   If len(strSuffix) Then
      strSolution = strSolution & "_" & strSuffix
   End If

   strPath = objFSO.BuildPath(strPath, strSolution)

   If Not objFSO.FolderExists(strPath) Then
      Wscript.Echo "Source solution folder """ & strPath & """ not found"
      Wscript.Quit
   End If

   strProject = objFSO.GetBaseName(strFile)

   If len(strSuffix) Then
      strProject = strProject & "_" & strSuffix
   End If

   strPath = objFSO.BuildPath(strPath, strProject)

   If Not objFSO.FolderExists(strPath) Then
      Wscript.Echo "Source project folder """ & strPath & """ not found"
      Wscript.Quit
   End If

   strPath = objFSO.BuildPath(strPath, "Release")

   If Not objFSO.FolderExists(strPath) Then
      Wscript.Echo "Source release folder """ & strPath & """ not found"
      Wscript.Quit
   End If

   strPath = objFSO.BuildPath(strPath, strFile)

   If Not objFSO.FileExists(strPath) Then
      Wscript.Echo "Source file """ & strPath & """ not found"
      Wscript.Quit
   End If

   Set objFile = objFSO.GetFile(strPath)
   strTargetFilePath = objFSO.BuildPath(objTarget.Path, strFile)

   'Wscript.Echo "Source:", strPath
   'Wscript.Echo "Destination:", objTarget.Path
   Wscript.Echo "Copying binary """ & objFSO.GetFileName(strPath) & """ to target folder"
   'Call objFSO.CopyFile(strPath, objTarget.Path)  'TODO: resolve overwrite issue
   objFile.Copy(strTargetFilePath)
End Sub
